package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.domain.basic.CustomerDepositGift;
import cc.yugu.yhdw.common.domain.basic.CustomerInOutMoney;
import cc.yugu.yhdw.common.domain.basic.DepositOrder;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.basic.AgentMapper;
import cc.yugu.yhdw.webserver.persistence.basic.CustomerInOutMoneyMapper;
import cc.yugu.yhdw.webserver.persistence.basic.PassportMapper;
import cc.yugu.yhdw.webserver.persistence.basic.DepositOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
/**
 * 客户充值订单业务
 * 
 * @Date:Created in 2018/7/3 15:11
 * @Author:
 * @Modified By: zjn 2018/7/3 15:11
 */ 
@Service
public class DepositOrderService {

    @Autowired
    DepositOrderMapper depositOrderMapper;
    @Autowired
    PassportMapper passportMapper;
    @Autowired
    CustomerInOutMoneyMapper customerInOutMoneyMapper;
    @Autowired
    AgentMapper agentMapper;
    /**
    * 根据主键查询表 bas_Deposit_order
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.basic.DepositOrder
    * @Author By: zjn 2018/7/3  15:12
    */ 
    public DepositOrder find(String id) {
        return depositOrderMapper.find(id);
    }
    /**
    * 分页
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/3  15:12
    */ 
    public Page findPage(DepositOrder search) {
        Page page = search.buildPage();
        page.setTotalItems(depositOrderMapper.findPageCount(search));
        search.setBeginIndex(page.getOffset());
        page.setResult(depositOrderMapper.findPageResult(search));

        return page;
    }
    /**
    * 指定字段插入数据局
    * 
    * @Param: [depositOrder]
    * @Return: int
    * @Author By: zjn 2018/7/3  15:12
    */ 
    public int insert(DepositOrder depositOrder) {
        return depositOrderMapper.insert(depositOrder);
    }
    /**
    * 更新订单（变更套餐？）
    * 
    * @Param: [depositOrder, giftList]
    * @Return: int
    * @Author By: zjn 2018/7/3  15:13
    */ 
    @Transactional(rollbackFor=Throwable.class)
    public int payOk(DepositOrder depositOrder, List<CustomerDepositGift> giftList) {
        // 根据主键和订单状态 更新订单状态和操作时间，
        int effect = depositOrderMapper.payOk(depositOrder.getId(), new Date(), DepositOrder.Status.INIT.getValue(), DepositOrder.Status.SUCCESS.getValue());
        if(effect > 0) {
            int giftMoney = 0;
            if(!giftList.isEmpty()) {
                // 充值优惠
                float gift = CustomerDepositGift.gift(giftList, depositOrder.getMoney() / 100f);
                if(gift > 0) {
                    giftMoney = (int) (gift * 100);
                }
            }

                if(passportMapper.updateBalance(depositOrder.getCustomerId(), depositOrder.getMoney() + giftMoney) > 0) {
                    CustomerInOutMoney customerInOutMoney = new CustomerInOutMoney();
                    customerInOutMoney.setCustomerId(depositOrder.getCustomerId());
                    customerInOutMoney.setBizType(CustomerInOutMoney.BizType.IN_MONEY_ONLINE.getValue());
                    customerInOutMoney.setMoney(Math.abs(depositOrder.getMoney()));
                    customerInOutMoney.setCreateTime(new Date());
                    customerInOutMoney.setSourceId(depositOrder.getId());
                    customerInOutMoneyMapper.insert(customerInOutMoney);

                }


        }

        return effect;
    }
}
